package com.tianqu.tms.entity;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.CopyFile;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsDetectPicDao;
import com.tianqu.tms.dto.TmsDetectPicDto;

/**
 * 检测图片entity
 * 
 * @author tianshifu
 * 
 */
public class TmsDetectPic extends TmsEntity {

	private static final long serialVersionUID = 1L;

	private int ID;// 图片ID
	private int carDetectId;// 检测ID
	private int chkModel;// 检测模式
	private int imgType;// 图片类型
	private String imgKey;// 图片位置
	private String ImgPath;// 图片路径

	public static TmsDetectPic getInstance() {
		TmsDetectPic entity = new TmsDetectPic();
		entity.setExisting(false);
		return entity;
	}

	public void delPic(int carDetectId) throws TmsDaoException {
		TmsDetectPicDao picDao = (TmsDetectPicDao) TmsDao
				.getInstance(TmsDao.TMS_DETECT_PIC);
		picDao.deleteTisPicInfoByDetectId(carDetectId);
	}

	/**
	 * 图片信息插入
	 */
	public void insertTisPic(TmsDetectPic entity) throws TmsDaoException {

		TmsDetectPicDao picDao = (TmsDetectPicDao) TmsDao.getInstance(TmsDao.TMS_DETECT_PIC);
		TmsDetectPicDto Tispic = new TmsDetectPicDto();
		Tispic.setCarDetectId(entity.getCarDetectId());
		Tispic.setChkModel(entity.getChkModel());
		Tispic.setImgType(entity.getImgType());
		Tispic.setImgKey(Integer.parseInt(entity.getImgKey()));
		Tispic.setPath(entity.getImgPath());
		picDao.insertTisPic(Tispic);
	}

	
	
	
	/**
	 * 服务器端图片信息录入
	 * 
	 */
	public void insertTisPicInfo(List<TmsDetectPic> list,String sysPath,int carDetectId,String serial,int chkModel) throws TmsDaoException {

		sysPath = sysPath.replace("/tmsweb", "");
		
		//对文件名进行处理
		//List<CarImage>  图片的list 传到daoimp层进行迭代添加到tms_pic表中
		List<TmsDetectPicDto> dtoList = new ArrayList<TmsDetectPicDto>();
		TmsDetectPicDto imgDto = null;
		File outFile = null;
		File inFile = null;
		if(list!=null)
		{
			for(int i=0;i<list.size();i++)
			{
				TmsDetectPic imgEntity = (TmsDetectPic)list.get(i);
				
				//只有上传的图片进行覆盖，不上传的不进行覆盖
				if(imgEntity.getImgPath().indexOf("carImageTmp") != -1)
				{
					//对每个path进行处理
					inFile = new File(sysPath+imgEntity.getImgPath());
				
					outFile = new File(sysPath + Registry.getConfig(TmsConstants.UPLOAD_CONTEXT_ROOT_KEY)+"/"+serial+"/");
					if (!outFile.exists()) {
						outFile.mkdirs();
					}
					outFile = new File(outFile+"/"+serial+carDetectId+chkModel+imgEntity.getImgType()+imgEntity.getImgKey()+".jpg");
					
					//boolean result = 
					CopyFile.copy(inFile, outFile);
				}
//				if(result){
					imgDto = new TmsDetectPicDto();
					imgDto.setImgKey(Integer.parseInt(imgEntity.getImgKey()));
					imgDto.setImgType(imgEntity.getImgType());
					imgDto.setCarDetectId(carDetectId);
					imgDto.setChkModel(chkModel);
					imgDto.setPath(imgPath(imgEntity.getImgPath(),serial,imgEntity.getImgKey(),String.valueOf(imgEntity.getImgType()),String.valueOf(carDetectId),String.valueOf(chkModel)));
//				}
				//ImageOperate.formatImg(inFile,outFile );//更改文件名
				dtoList.add(imgDto);
			}
		}
		
		//进行图片保存
		TmsDetectPicDao picDao = (TmsDetectPicDao) TmsDao.getInstance(TmsDao.TMS_DETECT_PIC);
		picDao.insertTisDetectPicInfo(dtoList);
	}
	
	
	/**
	 * 处理存入数据库中的图片路径
	 */
	private String imgPath(String path,String serial,String imgKey,String imgType,String carDetectId,String chkModel){
		String imageRootPath = "";
		
		return imageRootPath + Registry.getConfig(TmsConstants.UPLOAD_CONTEXT_ROOT_KEY) +"/"+serial+"/"+serial+carDetectId+chkModel+imgType+imgKey+".jpg";	 
	}
   
	/**
	 * 查询单条图片信息
	 * @param dto
	 * @return
	 * @throws TmsDaoException
	 */
    public TmsDetectPic getTisPic(TmsDetectPic dto)throws TmsDaoException{
    	TmsDetectPicDao picDao = (TmsDetectPicDao) TmsDao.getInstance(TmsDao.TMS_DETECT_PIC);
    	TmsDetectPicDto Tispic = new TmsDetectPicDto();
		Tispic.setCarDetectId(dto.getCarDetectId());
		Tispic.setChkModel(dto.getChkModel());
		Tispic.setImgType(dto.getImgType());
		Tispic.setImgKey(Integer.parseInt(dto.getImgKey()));
		List<TmsDetectPicDto> list =picDao.getTisPic(Tispic);
		if(list.size()>0){
			for(TmsDetectPicDto pic:list){
				TmsDetectPic TisPic = new TmsDetectPic();
				TisPic.setCarDetectId(pic.getCarDetectId());
				TisPic.setChkModel(pic.getChkModel());
				TisPic.setImgType(pic.getImgType());
				TisPic.setImgKey(String.valueOf(pic.getImgKey()));
				return TisPic;
			}
		}
    	return null;
    }
	
	/**
	 * 
	 * 根据车辆检测ID查询图片信息
	 * @throws TmsDaoException 
	 */
	public List<TmsDetectPic> findDetectPicInfoByDetectIdAndImgType(int carDetectId,int imgType) throws TmsDaoException
	{
		TmsDetectPicDao picDao = (TmsDetectPicDao) TmsDao.getInstance(TmsDao.TMS_DETECT_PIC);	
		List<TmsDetectPicDto> dtoList = picDao.findDetectPicInfoByDetectIdAndType(carDetectId,imgType);
		TmsDetectPic entity = null;
		List<TmsDetectPic> list = new ArrayList<TmsDetectPic>(); 
		if(dtoList.size()>0)
		{
			for(int i=0;i<dtoList.size();i++)
			{
				TmsDetectPicDto dto =  dtoList.get(i);
				entity = new TmsDetectPic();
				entity.setChkModel(dto.getChkModel());
				entity.setImgKey(String.valueOf(dto.getImgKey()));
				entity.setImgType(dto.getImgType());
				entity.setImgPath(dto.getPath());
				list.add(entity);
			}
		}
		return list;
	}
	
	/**
	 * 
	 * 根据车辆检测ID查询图片信息
	 * @throws TmsDaoException 
	 */
	public List<TmsDetectPic> findDetectPicInfoByDetectId(int carDetectId) throws TmsDaoException
	{
		TmsDetectPicDao picDao = (TmsDetectPicDao) TmsDao.getInstance(TmsDao.TMS_DETECT_PIC);	
		List<TmsDetectPicDto> dtoList = picDao.findDetectPicInfoByDetectId(carDetectId);
		TmsDetectPic entity = null;
		List<TmsDetectPic> list = new ArrayList<TmsDetectPic>(); 
		if(dtoList.size()>0)
		{
			for(int i=0;i<dtoList.size();i++)
			{
				TmsDetectPicDto dto =  dtoList.get(i);
				entity = new TmsDetectPic();
				entity.setID(dto.getId());
				entity.setCarDetectId(dto.getCarDetectId());				
				entity.setChkModel(dto.getChkModel());
				entity.setImgKey(String.valueOf(dto.getImgKey()));
				entity.setImgType(dto.getImgType());
				entity.setImgPath(dto.getPath());
				list.add(entity);
			}
		}
		return list;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {

	}

	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}

	public int getCarDetectId() {
		return carDetectId;
	}

	public void setCarDetectId(int carDetectId) {
		this.carDetectId = carDetectId;
	}

	public int getChkModel() {
		return chkModel;
	}

	public void setChkModel(int chkModel) {
		this.chkModel = chkModel;
	}

	public int getImgType() {
		return imgType;
	}

	public void setImgType(int imgType) {
		this.imgType = imgType;
	}



	public String getImgKey() {
		return imgKey;
	}

	public void setImgKey(String imgKey) {
		this.imgKey = imgKey;
	}

	public String getImgPath() {
		return ImgPath;
	}

	public void setImgPath(String imgPath) {
		ImgPath = imgPath;
	}

}
